package com.tool.hooker;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;

public class SettingProvider extends ContentProvider
{
    public SettingProvider()
    {
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        SharedPreferences sharedPreferences = getSharedPreferences(uri);

        SharedPreferences.Editor edit = sharedPreferences.edit();

        int count = 0;
        for (String name : uri.getQueryParameterNames())
        {
            if (sharedPreferences.contains(name))
                count++;

            edit.remove(name);
        }

        edit.commit();

        return count;
    }

    @Override
    public String getType(Uri uri)
    {
        return "settings";
    }

    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        SharedPreferences sharedPreferences = getSharedPreferences(uri);

        SharedPreferences.Editor edit = sharedPreferences.edit();

        for (String name : uri.getQueryParameterNames())
        {
            String value = uri.getQueryParameter(name);

            if ("null".equals(value))
            {
                edit.remove(name);
            }
            else if (value != null)
            {
                edit.putString(name, value);
            }
        }

        edit.commit();

        return uri;
    }

    @Override
    public boolean onCreate()
    {
        return true;
    }

    private SharedPreferences getSharedPreferences(String name)
    {
        return getContext().getSharedPreferences(name, 0);
    }

    private SharedPreferences getSharedPreferences(Uri uri)
    {
        return getSharedPreferences(uri.getPath().substring(1));
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder)
    {
        SharedPreferences sharedPreferences = getSharedPreferences(uri);

        String[] cls = new String[]{"n", "v"};

        MatrixCursor matrixCursor = new MatrixCursor(cls);

        for (String name : uri.getQueryParameterNames())
        {
            String value = uri.getQueryParameter(name);

            if (value != null)
            {
                String string = sharedPreferences.getString(name, null);
                if (string != null)
                    matrixCursor.addRow(new Object[]{name, string});
            }
        }

        return matrixCursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs)
    {
        SharedPreferences sharedPreferences = getSharedPreferences(uri);

        SharedPreferences.Editor edit = sharedPreferences.edit();

        int count = 0;

        for (String name : uri.getQueryParameterNames())
        {
            String value = uri.getQueryParameter(name);

            if (value != null)
            {
                if (sharedPreferences.contains(name))
                    count++;

                edit.putString(name, value);
            }
        }

        edit.commit();

        return count;
    }


}
